package com.orange.community.config;

import com.orange.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @author GaryGyp
 * 配置 --> 数据库 --> 调用
 */
@Configuration
public class QuartzConfig {

    /**
     * Spring底层有很多xxxFactoryBean
     * FactoryBean：可以简化Bean的实例化过程
     *   1. Spring通过 FactoryBean 封装了某一些 Bean 的实例化过程
     *   2. 将 FactoryBean 装配到 Spring容器里
     *   3. 将 FactoryBean 注入给其他的 Bean
     *   4. 该 Bean 得到的是 FactoryBean 所管理的对象实例
     *
     * BeanFactory就是容器的底层接口
      */

    /**
     * 刷新帖子分数任务
     */
    // 配置JobDetail
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    // 配置Trigger （SimpleTriggerFactoryBean， CronTriggerFactoryBean）
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        // 5min
        factoryBean.setRepeatInterval(1000 * 60 * 5);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }

}
